package org.example;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@Slf4j
@RestController
@RequestMapping("/api/rocketmq")
public class TestController {

    @Autowired
    private MessageProducer messageProducer;
    
    @Autowired
    private OrderService orderService;
    
    // 发送普通消息
    @PostMapping("/send")
    public String sendMessage(@RequestParam String message) {
            messageProducer.sendMessage("TEST_TOPIC", message);
        return "消息发送成功: " + message;
    }
    
    // 创建订单
    @PostMapping("/order/create")
    public String createOrder(@RequestParam String userId, 
                            @RequestParam BigDecimal amount) {
        orderService.createOrder(userId, amount);
        return "订单创建成功: userId=" + userId + ", amount=" + amount;
    }
    
    // 支付订单
    @PostMapping("/order/pay")
    public String payOrder(@RequestParam String orderNo) {
        orderService.payOrder(orderNo);
        return "订单支付成功: " + orderNo;
    }
    
    // 取消订单
    @PostMapping("/order/cancel")
    public String cancelOrder(@RequestParam String orderNo) {
        orderService.cancelOrder(orderNo);
        return "订单取消成功: " + orderNo;
    }
    
    // 发送延迟消息
    @PostMapping("/delay")
    public String sendDelayMessage(@RequestParam String message, 
                                 @RequestParam int delayLevel) {
        messageProducer.sendDelayMessage("TEST_TOPIC", message, delayLevel);
        return "延迟消息发送成功: " + message + ", delayLevel=" + delayLevel;
    }
    
    // 健康检查
    @GetMapping("/health")
    public String health() {
        return "RocketMQ Demo is running...";
    }
}